package com.ews.onlineexchange.controller;

import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.websocket.DeploymentException;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.BulletinService;
import com.ews.onlineexchange.service.CandidateSupplierSelector;
import com.ews.onlineexchange.service.DeterminedRecordService;
import com.ews.onlineexchange.service.qualifier.EndChooseWsEvent;
import com.ews.onlineexchange.service.qualifier.MessagePushWsEvent;
import com.ews.onlineexchange.service.qualifier.StartChooseWsEvent;

@Stateless
@Named
public class BulletinChooseContoller implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	@EndChooseWsEvent
	Event<Bulletin> endChooseWsEvent;
	private String formerBulletinId;
	@Inject
	@StartChooseWsEvent
	Event<Bulletin> startChooseWsEvent;
	private List<ServiceSupplier> suppliers = new ArrayList<>();
	@Inject
	EntityManager entityManager;
	public void endChooseBulletin() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String bulletinId = params.get("bulletinId");
		Bulletin bulletin = entityManager.find(Bulletin.class, bulletinId);
		endChooseWsEvent.fire(bulletin);
		
	}
	@Inject
	BulletinService bulletinService;
	@Transactional
	public void startChooseBulletin() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String bulletinId = params.get("bulletinId");
		Bulletin bulletin = entityManager.find(Bulletin.class, bulletinId);
		if(bulletin.getTradeStatus().equals(TradeStatus.Opened)) {
			if(bulletinService.updateBulletinChooseTime(bulletin)) {
				bulletinService.endBulletin(bulletin);
			}
			startChooseWsEvent.fire(bulletin);
		}
	}
	//测试消息推送
	@Inject
	@MessagePushWsEvent
	Event<String> messagePushWsEvent;
	public void pushTestMessage() throws DeploymentException, IOException, URISyntaxException {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String userIds = params.get("userIds");
		messagePushWsEvent.fire(userIds);
	}
	
	@Inject
	CandidateSupplierSelector candidateSupplierSelector;
	public List<ServiceSupplier> getSupplierByBulletinId(){
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String bulletinId = params.get("bulletinId");
        if(StringUtils.isNotBlank(bulletinId)) {
        	Bulletin bulletin = entityManager.find(Bulletin.class, bulletinId);
    		return candidateSupplierSelector.getResultList(bulletin);
        }else return new ArrayList<>();
		
	}
	@Inject
	DeterminedRecordService recordService;
	public List<ServiceSupplier> getSuppliers() {
		suppliers = getSupplierByBulletinId();
		for(int i = 0; i < suppliers.size() ; i++) {
			ServiceSupplier supplier = suppliers.get(i);
			if(recordService.isExistSupplierInRecord(supplier.getId())) {
				suppliers.remove(i);
				i--;
			}
		}
		return suppliers;
	}

	public void setSuppliers(List<ServiceSupplier> suppliers) {
		this.suppliers = suppliers;
	}

}
